package com.xhs.main.service.yfb.impl;

import com.xhs.main.dao.yfb.*;
import com.xhs.main.entity.yfb.*;
import com.xhs.main.service.yfb.YfbItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @create 2020-10-23 15:28
 */
@Service
public class YfbItemServiceImpl implements YfbItemService {
    @Autowired
    YfbItemMapper yfbItemMapper;  //项目表
    @Autowired
    YfbBookMapper yfbBookMapper;  //计划书表
    @Autowired
    YfbResponsibilityMapper yfbResponsibilityMapper;  //责任表
    @Autowired
    YfbGroupMapper yfbGroupMapper;  //分组表
    @Autowired
    YfbDesignMapper yfbDesignMapper;  //设计表
    @Autowired
    YfbTestMapper yfbTestMapper;  //测试表

    @Transactional
    @Override
    public void createItem(YfbItem yfbItem, YfbBook yfbBook, String yfbGroupName) {
        /*
            ！！！这三个参数的属性包含多个数据
         */
        //1.创建项目表
        yfbItem.setYfbItemId(UUID.randomUUID() + "");  //设置主键
        yfbItem.setYfbItemStatus("开发中");  //状态
        yfbItemMapper.insert(yfbItem);
        //2.创建计划书
        String[] getYfbBookDevelopsorts = yfbBook.getYfbBookDevelopsort().split(",");
        String[] getYfbBookBooksorts = yfbBook.getYfbBookBooksort().split(",");
        String[] getYfbBookContents = yfbBook.getYfbBookContent().split(",");
        String[] getYfbBookCreatetimes = yfbBook.getYfbBookCreatetime().split(",");
        String[] getYfbBookVersions = yfbBook.getYfbBookVersion().split(",");
        for (int i = 0; i < getYfbBookVersions.length; i++) {
            if(getYfbBookVersions[i] != null && !getYfbBookVersions[i].equals("")) {
                YfbBook record2 = new YfbBook(null,
                        yfbItem.getYfbItemId(),  //项目表主键
                        getYfbBookDevelopsorts[i],
                        getYfbBookBooksorts[i],
                        getYfbBookContents[i],
                        getYfbBookCreatetimes[i],
                        getYfbBookVersions[i],
                        null);
                yfbBookMapper.insert(record2);
            }
        }
        //3.根据组名查询分组表
        //传过来的组名可能有多个
        String[] yfbGroupNames = yfbGroupName.split(",");
        List<YfbGroup> groups = new ArrayList<>();
        for (int i = 0; i < yfbGroupNames.length; i++) {
            YfbGroupExample exception = new YfbGroupExample();
            YfbGroupExample.Criteria criteria = exception.createCriteria();
            criteria.andYfbGroupNameEqualTo(yfbGroupNames[i]);  //根据组名查
            criteria.andYfbGroupParentidNotEqualTo(-1);  //父id不为-1
            //先将小组人员查出来
            List<YfbGroup> newGroups = yfbGroupMapper.selectByExample(exception);
            groups.addAll(newGroups);
        }
        //再set进去
        for (int i = 0; i < groups.size(); i++) {
            YfbGroup group = groups.get(i);
            YfbResponsibility record = new YfbResponsibility(null,
                    yfbItem.getYfbItemId(),  //项目表主键
                    group.getYfbGroupName(),  //组名
                    group.getYfbGroupClass(),  //分类：软硬
                    group.getYfbGroupPeoplename(),  //人名
                    group.getYfbGroupPosition(),  //职位
                    group.getYfbGroupUsernumber());  //用户编号
            yfbResponsibilityMapper.insert(record);
        }

    }

    @Override
    public void updateItem(YfbItem yfbItem) {
        yfbItemMapper.updateByPrimaryKey(yfbItem);
    }

    @Override
    public List<YfbItem> queryAllItems() {
        YfbItemExample example = new YfbItemExample();
        List<YfbItem> yfbItems = yfbItemMapper.selectByExample(example);
        //设置项目表的域属性
        for (int i = 0; i < yfbItems.size(); i++) {
            setDomainAttributes(yfbItems.get(i));
        }
        return yfbItems;
    }

    @Override
    public YfbItem queryItemById(String id) {
        YfbItem yfbItem = yfbItemMapper.selectByPrimaryKey(id);
        //设置域属性
        return setDomainAttributes(yfbItem);
    }

    @Override
    public List<YfbItem> queryItemByLoginUserNumber(String yfbUserNumber) {
        //根据用户编号，查责任表
        YfbResponsibilityExample example = new YfbResponsibilityExample();
        YfbResponsibilityExample.Criteria criteria = example.createCriteria();
        criteria.andYfbRespUsernumberEqualTo(yfbUserNumber);
        List<YfbResponsibility> yfbResponsibilities = yfbResponsibilityMapper.selectByExample(example);
        List<YfbItem> yfbItems = new ArrayList<>();
        //查出负责的的项目主键，用主键查项目表。
        for (int i = 0; i < yfbResponsibilities.size(); i++) {
            YfbItem yfbItem = yfbItemMapper.selectByPrimaryKey(yfbResponsibilities.get(i).getYfbRespItemId());
            yfbItems.add(yfbItem);
        }
        //设置项目表的域属性
        for (int i = 0; i < yfbItems.size(); i++) {
            setDomainAttributes(yfbItems.get(i));
        }
        return yfbItems;
    }

    //设置项目表的域属性
    private YfbItem setDomainAttributes(YfbItem yfbItem)  {
        //1.根据项目表主键查计划书
        YfbBookExample example1 = new YfbBookExample();
        YfbBookExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andYfbBookItemIdEqualTo(yfbItem.getYfbItemId());
        List<YfbBook> yfbBooks = yfbBookMapper.selectByExample(example1);
        yfbItem.setYfbBooks(yfbBooks);
        //2.根据项目表主键查设计表
        YfbDesignExample example2 = new YfbDesignExample();
        YfbDesignExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andYfbDesignItemIdEqualTo(yfbItem.getYfbItemId());
        List<YfbDesign> yfbDesigns = yfbDesignMapper.selectByExample(example2);
        yfbItem.setYfbDesigns(yfbDesigns);
        //3.根据项目表主键查测试表
        YfbTestExample example3 = new YfbTestExample();
        YfbTestExample.Criteria criteria3 = example3.createCriteria();
        criteria3.andYfbTestItemIdEqualTo(yfbItem.getYfbItemId());
        List<YfbTest> yfbTests = yfbTestMapper.selectByExample(example3);
        yfbItem.setYfbTests(yfbTests);
        //4.根据项目表主键查负责人表
        YfbResponsibilityExample example4 = new YfbResponsibilityExample();
        YfbResponsibilityExample.Criteria criteria4 = example4.createCriteria();
        criteria4.andYfbRespItemIdEqualTo(yfbItem.getYfbItemId());
        List<YfbResponsibility> yfbResponsibilities = yfbResponsibilityMapper.selectByExample(example4);
        yfbItem.setYfbResponsibilities(yfbResponsibilities);
        return yfbItem;
    }
}
